package com.ymdx.service.impl;

import com.ymdx.dao.impl.BaseDaoImpl;
import com.ymdx.entity.User;
import com.ymdx.service.UserService;
import com.ymdx.util.Page;
import com.ymdx.util.tool.HqlUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by yangliwei on 2017/8/13.
 */
@SuppressWarnings("JpaQlInspection")
@Service("userService")
@Transactional(propagation = Propagation.REQUIRED)
public class UserServiceImpl extends BaseServiceImpl<User, Long> implements UserService {

    @Resource
    BaseDaoImpl userDao;

    @Override
    public User getUserByMobile(String mobile) {
        //这里不要添加valid = true 当用户被删除时，必须联系管理员，让管理员开启
        String hql = "from User where mobile = ?";
        List<User> result = (List<User>) hibernateTemplate.find(hql, mobile);
        if (result.isEmpty()) {
            return null;
        }
        return result.get(0);
    }

    @Override
    public Page<User> page(Page<User> page, Map<String, Object> args) {
        StringBuilder hql = new StringBuilder("from User where valid = true ");
        List params = new ArrayList();

        HqlUtil.andHql(hql,args,params);
        return userDao.page(hql.toString(), page, params.toArray());
    }


}
